home *** CD-ROM | disk | FTP | other *** search
/ MacWorld 1999 January - Disc 2 / Macworld (1999-01) (Disk 2).dmg / Serious Demos / Symbolic Composer 4.2 / Environment / Projects / Questions & Answers / Q&A Basic / Delayed evaluation < prev    next >
Text File  |  1998-10-26  |  1KB  |  74 lines

  1. >i was wondering how to use scom functions with delayed evaluation arguments.
  2. >that is, instead of the argument being evaluated, and then passed as a
  3. >parameter to the function, i want the function to take the argument as a
  4. >function, and evaluate it during the function's evaluation.  does this make
  5. >sense?
  6.  
  7. You can make it a macro and evaluate the parameter with eval. Here you
  8. supply the function call (rnd2 0 1) to the delayed-evaluation which
  9. then evaluates and prints the result 10 times. Notice that since this
  10. is a macro it will return a value which will be evaluated, since 
  11. macros are usually used for making new Lisp forms which expand and
  12. evaluate. To overcome this make your macro returning nil.
  13.  
  14. (defmacro delayed-evaluation (function)
  15.   (dotimes (i 10)
  16.     (print (eval function)))
  17.   nil)
  18.  
  19. (delayed-evaluation (rnd2 0 1))
  20.  
  21. 0.7697816208819859 
  22. 0.915418354110443 
  23. 0.8008900594140869 
  24. 0.9490920328971697 
  25. 0.5024546815548092 
  26. 0.7643523601145716 
  27. 0.8911941803817172 
  28. 0.7534653782931855 
  29. 0.49140359536977485 
  30. 0.33869340586534236 
  31. nil
  32. ?
  33.  
  34. If you want to evaluate any number of arguments in a similar way
  35. then make it this way.
  36.  
  37. (defmacro delayed-evaluation2 (&rest body)
  38.   (dotimes (i 10)
  39.     (eval `(progn ,@body)))
  40.   nil)
  41.  
  42. (delayed-evaluation2 (print 1) (print 2) (print 3))
  43.  
  44.